﻿namespace EFS.RC.SupervisorCentral
{
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : System.Windows.Application
	{
		#region Locals
		/// Lync communicator service name
		public const System.String LyncCommunicatorProcessName = "communicator";

		/// allow only one single instance
		private System.Threading.Mutex mutexObj = null;
		#endregion

		protected override void OnStartup(System.Windows.StartupEventArgs e)
		{
			// call base
			base.OnStartup(e);

			System.Boolean createdNew = false;
			mutexObj = new System.Threading.Mutex(true, "EFS.RC.SupervisorCentral", out createdNew);

			// test
			if (!createdNew)
			{
				System.Windows.Forms.MessageBox.Show("Another instance is already running.", "Still running", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
				Shutdown();
			}

			try
			{
				// log
				EFS.EventLog.Log.LogInfo("EFS.RC.SupervisorCentral Application starting", null);

				// start log
				EFS.EventLog.Log.StartAsyncLogger(false);
			}
			catch (System.Exception ex)
			{
				System.Windows.Forms.MessageBox.Show(ex.Message, "Fatal Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
				Shutdown();
				return;
			}

			// make image source cache
			EFS.RC.SupervisorCentral.Resources.Caching.MakeImageSourcesCache();

			//System.IO.StreamReader sr = new System.IO.StreamReader(@"D:\DEVELOPMENT\ExeterFriendlySociety\ESS_REPO\devel\EFS.RC.SupervisorCentral\xml.txt");
			//System.String xml = sr.ReadToEnd();
			//sr.Close();
			//EFS.RC.CC.Messages.Message.Deserialize(xml);
		}

		protected override void OnExit(System.Windows.ExitEventArgs e)
		{
			base.OnExit(e);

			// stop log
			EFS.EventLog.Log.StopAsyncLogger();

			// keep alive
			System.GC.KeepAlive(mutexObj); // important!
		}
	}

}